package com.service.socket.protocol;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.service.bean.garbage.GarbageData;
import com.service.other.Logger;

/**
 * 垃圾称重协议数据区解析
 * @author lj
 *
 */
public class GarbageDataProtocol {

	private static String protocol = "(?<driverNo>\\w*?)00(?<collectionId>\\w*?)00(?<firstTrashId>\\w*?)00(?<firstTrashWeight>\\w{8})(?<secondTrashId>\\w*?)00(?<secondTrashWeight>\\w{8})(?<trashNum>\\w{4})(?<year>\\w{2})(?<month>\\w{2})(?<day>\\w{2})(?<hour>\\w{2})(?<minute>\\w{2})(?<second>\\w{2})(?<latitude>\\w{8})(?<longtitude>\\w{8})";
	
	public static GarbageData Parser(String dataArea) {
		GarbageData data = new GarbageData();
		try {
			Pattern p = Pattern.compile(protocol);
			Matcher m = p.matcher(dataArea);
			if (m.find()) {
				Field[] fields = data.getClass().getFields();
				//this.getClass().getDeclaredFields();全部字段
				//this.getClass().getFields();public字段
				for (int i = 0; i < fields.length; i++) {
					fields[i].setAccessible(true);
					String name = fields[i].getName();
					try {
						if (name.equals("driverNo") || name.equals("firstTrashId") || name.equals("secondTrashId")) {
							fields[i].set(data, m.group(name));
						} else {
							fields[i].set(data, Conversion.reversal(m.group(name)));
						}
					} catch (Exception e) {
						Logger.err(e);
					}
				}
			}
		} catch (Exception e) {
			Logger.err(dataArea);
			Logger.err(e);
		}
		return data;
	}
	
}
